<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

var copyRandomList = function(head) {
    if(!head) return null
    let p = head
    let l = new Node()
    let l1 = l
    while(p){
        let l2 = new Node(p.val)
        l1.next = l2
        l1 = l1.next
        p = p.next
    }
    p = head
    let p1 = head
    l1 = l.next
    let l2 = l.next
    let temp = 0
    while(p){
        if(p.random){
            while(p1){
                if(p.random === p1){
                    break
                }
                temp++
                p1 = p1.next
            }
            for(let k = 0; k<temp ; k++){
                l1 = l1.next
            }
            l2.random = l1
        }
        temp = 0
        p1 = head
        l1 = l.next
        p = p.next
        l2 = l2.next
    }
    
    return l.next
};

    </script>
</body>
</html>